<?php
/**
 * Smarty View
 *
 * PHP version 5
 *
 * @category   Web
 * @package    App
 * @subpackage View
 * @author     Logger Testenborg <logger.testenborg@data2people.com>
 * @copyright  http://data2people.com
 * @license    Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt
 * @version    SVN: $Id: $
 *
 */

/**
 * Includes
 */
require_once 'Smarty.class.php';

class App_View_Smarty extends Zend_View_Abstract
{
    /**
     *  Smarty object
     *
     * @var Smarty
     */
    protected $_smarty;

    /**
     * @var string Name of template to be rendered
     *
     */
    private $_template_name;

    /**
     * Sets up Smarty engine using settings from configuration file
     *
     * @param array $data not used for now
     *
     * @return void
     *
     */
    public function __construct(array $data = array())
    {
        /**
         * @todo Check this if construction
         */
        if (array_key_exists('right_delimiter', $data)) {
            $config = $data;
        } elseif (Zend_Registry::isRegistered('smarty_config')) {
            $config = Zend_Registry::get('smarty_config');
        } else {
            throw new Zend_Exception('Smarty is not initiallized!');
        }

        /**
         * Here we should set up current module determination as each
         * of them has its own folder
         */
        $viewSettings = array(
            'encoding'   => 'UTF-8',
            'scriptPath' => array($config['template_dir'])
        );
        
        parent::__construct($viewSettings);

        $this->_smarty                  = new Smarty();
        $this->_smarty->caching         = $config['caching'];
        $this->_smarty->cache_lifetime  = $config['cache_lifetime'];
        $this->_smarty->template_dir    = $config['template_dir'];
        $this->_smarty->compile_dir     = $config['compile_dir'];
        $this->_smarty->config_dir      = $config['config_dir'];
        $this->_smarty->cache_dir       = $config['cache_dir'];
        $this->_smarty->debugging       = $config['debugging'];
        $this->_smarty->force_compile   = $config['force_compile'];
        $this->_smarty->compile_check   = $config['compile_check'];
        $this->_smarty->left_delimiter  = $config['left_delimiter'];
        $this->_smarty->right_delimiter = $config['right_delimiter'];

        $this->setFilter(null);
        
        $this->translate()->setTranslator($this->_tr);
              
        $this->_smarty->assign('View', $this);

        $this->headTitle('Tourstart')
             ->setSeparator(' / ');
    }


    /**
     * Register postfilter for template
     *
     * @param string $name Function name
     *
     * @throws Zend_View_Exception if template_name is not set
     * @return void
     */
    public function register_postfilter($name)
    {
        $this->_smarty->register_outputfilter($name);
    }

    /**
     * Register Object for template
     *
     * @param string $name Function name
     *
     * @throws Zend_View_Exception if template_name is not set
     * @return void
     */
    public function register_object($object_name, $object){
        $this->_smarty->register_object($object_name, $object);
    }


    /**
     * Renders and displays template
     *
     * @param string $template_name Template name without extension (before .tpl)
     *
     * @throws Zend_View_Exception if template_name is not set
     * @return void
     */
    protected function _run($template_name = null)
    {
        $this->_smarty->display($template_name);
    }

    /**
     * App_View_Smarty::assign('name', $value) assigns
     * a variable called 'name' with the corresponding $value.
     *
     * App_View_Smarty::assign($array) assigns the array keys as variable
     * names (with the corresponding array values).
     *
     * @param string|array $var   The assignment strategy to use.
     * @param mixed        $value If assigning a named variable, use this
     *                               as the value.
     *
     * @throws Zend_View_Exception If $spec is neither a string nor an array
     * @return void
     **/
    public function assign($var, $value = null)
    {
        if (is_string($var)) {
            $this->_smarty->assign($var, $value);
        } elseif (is_array($var)) {
            foreach ($var as $key => $value) {
                $this->_smarty->assign($key, $value);
            }
        } else {
            throw new Zend_View_Exception(__METHOD__.' expects a'
            .' string or array, got ').gettype($var);
        }
    }

    /**
     * Escape symbols
     *
     * @param string|integer|array $var Variable(s) to escape
     *
     * @return string|integer|array
     */
    public function escape($var)
    {
        if (is_string($var)) {
            return parent::escape($var);
        } elseif (is_array($var)) {
            foreach ($var as $key => $val) {
                $var[$key] = $this->escape($val);
            }
            return $var;
        } else {
            return $var;
        }
    }

    /**
     * Displays $name template
     *
     * @param string $name Template name
     *
     * @return string
     */
    public function output($name)
    {
        $this->_template_name = $name;
        try {
            echo parent::render($this->_template_name);
        } catch(Zend_View_Exception $e) {
            //$this->logger->debug("View Exception: ".$e.getMessage());
        } catch(Zend_Controller_Dispatcher_Exception $e) {
            //$this->logger->debug("View Exception: ".$e.getMessage());
        }


    }
    /**
     * Check if $template is cached
     *
     * @param string $template Template name
     *
     * @return boolean
     */
    public function isCached($template)
    {
        //$this->logger->debug("View_Smarty:isCached");
        if ($this->_smarty->is_cached($template)) {
            return true;
        }
        return false;
    }
    
    /**
     * Set caching
     *
     * @param boolean $caching Whether to cache or not
     *
     * @return void
     */
    public function setCaching($caching)
    {
        $this->_smarty->caching = $caching;
    }

    /**
     * Get assigned template variables
     *
     * @param unknown $vars ????
     *
     * @return array of assigned variables
     */
    public function getVars($vars = null)
    {
        return $this->_smarty->get_template_vars($vars);
    }

}